home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 101_01 / euclid.c < prev    next >
Text File  |  1985-11-13  |  1KB  |  37 lines

  1. /* This is an implementation of the algorithm given
  2. in Knuth Vol #1 describing a solution (by Euclid) to the problem
  3. of finding the greatest common denominator for two given
  4. integers.
  5. */
  6.  
  7. main ()
  8. char *string[10];
  9. unsigned r,m,n,x;
  10. unsigned temp;
  11. {
  12.          while (1)     {
  13.         printf("\nEnter first integer\n");
  14.         m = atoi(gets(string));
  15.         printf("Enter second integer\n");
  16.         n = atoi(gets(string));
  17.         if (m < n)  {
  18.            temp = m;
  19.            m = n;
  20.            n = temp;
  21.         }
  22.         r = m % n;
  23.         x = 1;
  24.         while (x > 0)    {
  25.              if (r == 0)  {
  26.             printf("\nLargest common divisor = %u", n);
  27.             x = 0;
  28.              }
  29.              else  {
  30.                m = n;
  31.                n = r;
  32.                r = m % n;
  33.              }
  34.         }
  35.          }
  36. }
  37.